package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.limao.DBDataClass.HomeAreaPlotDataClass;
import com.limao.DBProvider.HomeMenu_ColumProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class HomeStructureCacheManager extends BasicModule{
	
	private HomeMenu_ColumProvider provider;
	private static HomeStructureCacheManager instance;

    public static HomeStructureCacheManager getInstance() {
        return instance;
    }
 
    private Cache<String, String> HomeMenu_ColumCache;

    public HomeStructureCacheManager() {
    	super("HomeMenu_ColumCacheManager");
    	HomeMenu_ColumCache = CacheFactory.createCache("cd_HomeMenu_ColumCache");
    	
    	initProvider();
    }
    
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    public void ClearObjectCache()
    {
    	HomeMenu_ColumCache.clear();
    } 
	
    public void ClearObjectCache(String areaid,String columid)
    {
    	HomeMenu_ColumCache.remove("A"+areaid+"C"+columid);
    }  
    
    public List<HomeAreaPlotDataClass> Load_HomePlot_ByAreaidAndColumid(String areaid,String columid)
    {
    	
    	List<HomeAreaPlotDataClass> list=new ArrayList<HomeAreaPlotDataClass>();
    	if(CacheClass.isEmpty(columid) || CacheClass.isEmpty(areaid) )
    	{
    		return list;
    	}
    	String jsonStr=HomeMenu_ColumCache.get("A"+areaid+"C"+columid);
    	if(CacheClass.isEmpty(jsonStr))
    	{
    		list=provider.Load_HomePlot_ByAreaidAndColumid(areaid,columid);
    		if(list!=null && list.size()>0)
    		{
    			jsonStr=JSON.toJSONString(list);
    			HomeMenu_ColumCache.put("A"+areaid+"C"+columid, jsonStr);
    		}
    	}
    	else
    	{
    		HomeAreaPlotDataClass[] nodes = JSON.parseObject(jsonStr, new TypeReference<HomeAreaPlotDataClass[]>(){});    
    		list.addAll(Arrays.asList(nodes));
    	}
     	return list;
    }

   
    
//******************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =HomeMenu_ColumProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }
  
}

